package com.github.dice.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.dice.constant.ErrorEnums;
import com.github.dice.dto.OpenRoomDTO;
import com.github.dice.dto.ResultDTO;
import com.github.dice.dto.RoomDTO;
import com.github.dice.dto.SelectDTO;
import com.github.dice.dto.SelectListDTO;
import com.github.dice.entity.PlayerAndDiceMapping;
import com.github.dice.entity.Room;
import com.github.dice.entity.RoomOwner;
import com.github.dice.exception.BusinessException;
import com.github.dice.service.IRoomOwnerService;
import com.github.dice.service.IRoomService;

@RestController
@RequestMapping("/roomOwner")
public class RoomOwnerController extends BaseController {

    @Resource
    private IRoomService      roomService;

    @Resource
    private IRoomOwnerService roomOwnerService;

    @PostMapping("/openRoom")
    public ResultDTO openRoom(@RequestBody OpenRoomDTO openRoomDTO, HttpServletRequest request) {
        RoomOwner roomOwner = super.getRoomOwnerBySession(request);
        if (null != roomOwner) {
            openRoomDTO.setRoomOwnerName(roomOwner.getRoomOwnerName());
        } else {
            JSON.toJSONString(new ResultDTO<>(new BusinessException(ErrorEnums.E_6004)));
        }
        RoomDTO roomDTO = roomService.openRoom(openRoomDTO);
        return new ResultDTO<>(roomDTO);
    }

    @PostMapping("/roomOwnerLogin")
    public ResultDTO roomOwnerLogin(@RequestParam("roomOwnerName") String roomOwnerName,
                                    @RequestParam("roomOwnerPwd") String roomOwnerPwd, HttpServletRequest request) {

        RoomOwner roomOwner = roomOwnerService.getRoomOwnerByNameAndPwd(roomOwnerName, roomOwnerPwd);
        if (!ObjectUtils.isEmpty(roomOwner)) {
            super.setRoomOwnerSession(request, roomOwner);
            roomOwner.setRoomOwnerPwd(StringUtils.EMPTY);
            return new ResultDTO<>(roomOwner);
        } else {
            return new ResultDTO(ErrorEnums.E_6004);
        }
    }

    @PostMapping("/addRoomOwner")
    public ResultDTO addRoomOwner(@RequestBody RoomOwner roomOwner, HttpServletRequest request) {
        roomOwnerService.addRoomOwner(roomOwner);
        roomOwner.setRoomOwnerPwd(StringUtils.EMPTY);
        if (!ObjectUtils.isEmpty(roomOwner)) {
            super.setRoomOwnerSession(request, roomOwner);
            roomOwner.setRoomOwnerPwd(StringUtils.EMPTY);
            return new ResultDTO<>(roomOwner);
        } else {
            return new ResultDTO(ErrorEnums.E_6003);
        }
    }

    @RequestMapping("/roomOwnerList")
    public SelectListDTO roomOwnerList(@RequestParam(value = "name", required = false) String name,
                                       HttpServletRequest request) {
        RoomOwner roomOwner = super.getRoomOwnerBySession(request);
        if (!roomOwner.getRoomOwnerName().equals(name)) {
            throw new BusinessException(ErrorEnums.E_6006);
        }
        SelectListDTO selectListDTO = new SelectListDTO();
        List<RoomOwner> roomOwners = roomOwnerService.queryRoomOwnerList(name);
        List<SelectDTO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(roomOwners)) {
            roomOwners.forEach(r -> {
                SelectDTO selectDTO = new SelectDTO();
                selectDTO.setId(r.getRoomOwnerName());
                selectDTO.setText(r.getRoomOwnerName());
                list.add(selectDTO);
            });
            selectListDTO.setResults(list);
        }
        return selectListDTO;
    }

    @RequestMapping(value = "/showResult")
    public ResultDTO showResult(@RequestBody Room room, HttpServletRequest request) {
        RoomOwner roomOwner = super.getRoomOwnerBySession(request);
        if (!roomOwner.getRoomOwnerName().equals(room.getRoomOwnerName())) {
            throw new BusinessException(ErrorEnums.E_6006);
        }
        Boolean isAllRoll = roomOwnerService.checkAllRoll(room.getRoomNumber());
        if (!isAllRoll) {
            throw new BusinessException(ErrorEnums.E_7002);
        }
        List<PlayerAndDiceMapping> list = roomOwnerService.showResult(room.getRoomNumber(), room.getRoomOwnerName());
        return new ResultDTO<>(list);
    }
}
